package 图形化界面;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class SquareEquation {
    double a, b, c;
    double root1, root2;

    public void setA(double a) {
        this.a = a;
    }

    public void setB(double b) {
        this.b = b;
    }

    public void setC(double c) {
        this.c = c;
    }

    public double getRootOne() throws NoRealRootException,
            NoSquareEquationException {
        if (a != 0) {
            double disk = b * b - 4 * a * c;
            if (disk >= 0) {
                root1 = (-b + Math.sqrt(disk)) / (2 * a);
            } else {
                throw new NoRealRootException("没有实根");
            }
        } else {
            throw new NoRealRootException("不是二次方程");
        }
        return root1;
    }

    public double getRootTwo() throws NoRealRootException,
            NoSquareEquationException {
        if (a != 0) {
            double disk = b * b - 4 * a * c;
            if (disk >= 0) {
                root2 = (-b + Math.sqrt(disk)) / (2 * a);
            } else {
                throw new NoRealRootException("没有实根");
            }
        } else {
            throw new NoRealRootException("不是二次方程");
        }
        return root2;
    }
}

    class NoRealRootException extends  Exception{
        String message;
        NoRealRootException(String s){
            message = s;
        }

        public String getMessage(){
            return message;
        }
    }

    class NoSquareEquationException extends  Exception{

        String message;

        NoSquareEquationException(String s){
            message = s;
        }

        public String getMessage(){
            return message;
        }
    }

    class EquationFrame extends Frame implements ActionListener{
        SquareEquation equation;
        TextField textA,textB,textC;
        TextArea showRoots;
        Button controlButton;

        public EquationFrame(){

            equation = new SquareEquation();
            textA = new TextField(8);
            textB = new TextField(8);
            textC = new TextField(8);
            showRoots = new TextArea();
            controlButton = new Button("确定");
            Panel pNorth = new Panel();

            pNorth.add(new Label("二次项系数"));
            pNorth.add(textA);

            pNorth.add(new Label("一次项系数"));
            pNorth.add(textB);

            pNorth.add(new Label("常数项系数"));
            pNorth.add(textC);

            pNorth.add(controlButton);
            controlButton.addActionListener(this);
            add(pNorth,BorderLayout.NORTH);
            add(showRoots,BorderLayout.CENTER);
            setBounds(100,100,630,160);
            setVisible(true);
            validate();
            addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
        }

        public void actionPerformed(ActionEvent e){
            try{
                double a = Double.parseDouble(textA.getText());
                double b = Double.parseDouble(textB.getText());
                double c =Double.parseDouble(textC.getText());
                equation.setA(a);
                equation.setB(b);
                equation.setC(c);
                textA.setText(""+a);
                textB.setText(""+b);
                textC.setText(""+c);
                showRoots.append("\n根："+equation.getRootOne());
                showRoots.append(" 根："+equation.getRootTwo());
            }catch(Exception ex){
                showRoots.append("\n"+ex+"\n");
            }
        }


    }


public class 求平方根 {
    public static void main(String[] args) {
        EquationFrame frame = new EquationFrame();
    }
}